package com.qdev.apps.core.persistence.impl.interceptor;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;

import net.sf.hibernate.CallbackException;
import net.sf.hibernate.Interceptor;
import net.sf.hibernate.type.Type;

import com.qdev.apps.core.domain.AuditInfo;
import com.qdev.apps.core.domain.Auditable;

/**
 * @author Quake Wang
 * @since 2004-10-27
 * @version $Revision: 1.1 $
 */
public abstract class AbstractAuditInterceptor implements Interceptor, Serializable {

    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
            throws CallbackException {
        if (entity instanceof Auditable) {
            AuditInfo ai = ((Auditable) entity).getAuditInfo();
            if (ai == null) {
                ai = new AuditInfo();
                ((Auditable) entity).setAuditInfo(ai);
            }
            ai.setLastUpdated(new Date());
            ai.setUpdatedBy(getUserId());
        }
        return false;
    }

    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {
        if (entity instanceof Auditable) {
            AuditInfo ai = ((Auditable) entity).getAuditInfo();
            if (ai == null) {
                ai = new AuditInfo();
                ((Auditable) entity).setAuditInfo(ai);
            }            
            ai.setCreated(new Date());
            ai.setCreatedBy(getUserId());
        }
        return false;
    }

    public abstract String getUserId();

    public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     *  
     */

    public void onDelete(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] types) throws CallbackException {
        // TODO Auto-generated method stub

    }

    /**
     *  
     */

    public void preFlush(Iterator arg0) throws CallbackException {
        // TODO Auto-generated method stub

    }

    /**
     *  
     */

    public void postFlush(Iterator arg0) throws CallbackException {
        // TODO Auto-generated method stub

    }

    /**
     *  
     */

    public Boolean isUnsaved(Object arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     *  
     */

    public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, Type[] types) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     *  
     */

    public Object instantiate(Class arg0, Serializable arg1) throws CallbackException {
        // TODO Auto-generated method stub
        return null;
    }

}